package brave;

import org.bukkit.entity.Player;

public class TeamWallSign implements IWallSign
{
	private Integer teamId = null;
	private Integer gameId = null;
	
	public TeamWallSign(int game, int team)
	{
		this.teamId = team;
		this.gameId = game;
	}
	
	public int getTeamNum()
	{
		return this.teamId;
	}

	@Override
	public void onPlayerInteract(WallPlayer wallPlayer)
	{
		
		WallGame wallGame = Brave.getWallGame(gameId);
		Player player = wallPlayer.getBukkitPlayer();
		

		if (!wallGame.isGameReadyToAddPlayers())
		{
			player.sendMessage("BRAVE: Sorry the world is reloading");
			player.sendMessage("BRAVE: try again in a few seconds");
		}
		
		if (wallGame.hasStarted())
		{
			player.sendMessage("BRAVE: Sorry but this game has already started");
			player.sendMessage("BRAVE: You can still observe it");
			return;
		}
				
		// atempt to add a player to the game
		Integer teamSlot = wallGame.addPlayer(wallPlayer, teamId);
		
		// if we found a slot for the player send them to the game
		if (teamSlot != null)
		{
			wallPlayer.setState(WallPlayerState.IN_GAME);
			wallPlayer.setGameId(gameId);
			wallPlayer.setTeamId(teamId);

			player.teleport(Brave.getTeamLocation(gameId, teamId, teamSlot));
			
			// if the game has an even amount of players per team, start the game
			if (wallGame.isGameReadyToStart())
			{
				wallGame.start();
			}
		}
		else
		{
			player.sendMessage("Brave: Sorry but this team is full");
		}
	}

	@Override
	public Integer getGameId()
	{
		return this.gameId;
	}

	@Override
	public Integer getTeamId()
	{
		return this.teamId;
	}

}
